package io.renren.modules.hos.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.utils.ShiroUtils;
import io.renren.modules.hos.entity.HosUserEntity;
import io.renren.modules.hos.entity.PatientEntity;
import io.renren.modules.hos.form.AppointmentForm;
import io.renren.modules.hos.service.HosUserService;
import io.renren.modules.hos.service.PatientService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.hos.entity.AppointmentEntity;
import io.renren.modules.hos.service.AppointmentService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 预约表
 *
 * @author ryf
 * @email 2559228480@qq.com
 * @date 2021-12-21 13:40:39
 */
@RestController
@RequestMapping("hos/appointment")
public class AppointmentController {
    @Autowired
    private AppointmentService appointmentService;
    @Autowired
    private HosUserService hosUserService;
    @Autowired
    private PatientService patientService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = appointmentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("hos:appointment:info")
    public R info(@PathVariable("id") Long id){
		AppointmentEntity appointment = appointmentService.getById(id);

        return R.ok().put("appointment", appointment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("hos:appointment:save")
    public R save(@RequestBody AppointmentEntity appointment){
		appointmentService.save(appointment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("hos:appointment:update")
    public R update(@RequestBody AppointmentEntity appointment){
		appointmentService.updateById(appointment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("hos:appointment:delete")
    public R delete(@RequestBody Long[] ids){
		appointmentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 病人查看自己的预约记录
     */
    @RequestMapping("/patientList")
    public R queryPatientAppointment(@RequestBody Map<String, Object> params){
        HosUserEntity user = hosUserService.getById(ShiroUtils.getHosUserId());
        if (user.getAuthIdentity() != 1){
            return R.error().put("data", "您无权限");
        }else {
            PatientEntity patientEntity = patientService.selectPatient(user.getId());
            PageUtils page = appointmentService.queryPageByPatientId(patientEntity.getId(), params);
            return R.ok().put("page", page);
        }
    }

    /**
     * 添加预约
     */
    @RequestMapping("/add")
    public R addPatientAppointment(@RequestBody AppointmentForm form){
        HosUserEntity hosUserEntity = hosUserService.getById(ShiroUtils.getHosUserId());
        if (hosUserEntity.getAuthIdentity() != 1){
            return R.error().put("data", "您无权限");
        }else {
            PatientEntity patientEntity = patientService.selectPatient(hosUserEntity.getId());
            AppointmentEntity appointmentEntity = appointmentService.addAppointment(patientEntity.getId(), form);
            if (appointmentEntity != null){
                return R.ok().put("data", appointmentEntity);
            }else {
                return R.error();
            }
        }
    }


    /**
     * 取消预约
     * @param appointmentId
     * @return
     */
    @RequestMapping("/quit/{id}")
    public R quitAppointment(@PathVariable("id") Long appointmentId){
        AppointmentEntity appointmentEntity = appointmentService.getById(appointmentId);
        appointmentEntity.setOrderStatus(1);
        appointmentService.updateById(appointmentEntity);

        return R.ok().put("data", "取消成功");
    }

}
